<?php
session_start();

//require_once("includes/connect.php");
//require_once("includes/function.php");


$db = NEW myDBs();
$db->selectDB("account");

$sql = "SELECT status,lvl1_id, pet_id, tbl_salary.panngan2_id, panngan2_name, tbl_salary.panngan3_id, panngan3_name, ex1_id, ex2_id, ex3_id, expenses1_name, expenses2_name, expenses2_name, expenses3_name, SUM( salary ) AS salary, verify_status, approved_status,status
		FROM tbl_salary, panngan2, panngan3, expenses_lvl1, expenses_lvl2, expenses_lvl3
		WHERE lvl1_id =  '" . $_SESSION["lvl1_id"] . "'
		AND tbl_salary.panngan2_id = panngan2.panngan2_id
		AND tbl_salary.panngan3_id = panngan3.panngan3_id
		AND tbl_salary.ex1_id = expenses_lvl1.expenses1_id
		AND tbl_salary.ex2_id = expenses_lvl2.expenses2_id
		AND tbl_salary.ex3_id = expenses_lvl3.expenses3_id
		GROUP BY status,lvl1_id, pet_id, tbl_salary.panngan2_id, panngan2_name, tbl_salary.panngan3_id, panngan3_name, ex1_id, ex2_id, ex3_id, expenses1_name, expenses2_name, expenses2_name, expenses3_name, verify_status, approved_status,status 
		ORDER BY pet_id";

//echo $sql;
$result = $db->runSQL($sql);
//$rows = $db1->fetch_object($result);
?>

<script type="text/javascript">
    $(document).ready(function() {
        $("#example").dataTable();

        /*
         $("#add_new").click(function() {
         //alert("adfs");
         $('<div>').dialog2({
         autoOpen: true,
         title: "เพิ่มฎีกา",
         content: "dlg_add_pet.php",
         id: "modal-chkJob"
         });
         });
         */

    });

    /*
     function edit(pet_id)  {
     //alert("adfs");
     $('<div>').dialog2({
     autoOpen: true,
     title: "แก้ไขฎีกา",
     content: "dlg_add_pet.php?utility_id=" + utility_id,
     id: "modal-chkJob"
     });
     
     }
     */

//    function del(pet_id) {
//        //alert("adfs");
//        if (confirm("คุณแน่ใจที่ต้องการลบรายการนี้ใช่ไหม?"))
//        {
//            $.post("process_petition_salary.php", {pet_id: pet_id, task: 'del'}, function() {
//                window.location.href = "index.php?pages=frm_petition_salary&pageid=1";
//            });
//        }
//
//    }

    function cancel(pet_id) {
        if (confirm("คุณแน่ใจใช่ไหมที่ต้องการ ยกเลิก ฎีกานี้")) {

            $('<div>').dialog2({
                autoOpen: true,
                title: "ยกเลิกฎีกา",
                content: "dlg_cancel.php?tblname=tbl_salary&id=" + pet_id,
                id: "modal-cancel"
            });
        }
    }
    
    function approvedPetition(pet_id) {
        var task = "approved";

        $('<div>').dialog2({
            autoOpen: true,
            title: "อนุมัติ",
            content: "dlg_date_approved.php?pet_id=" + pet_id + "&task=" + task,
            id: "modal-date"
        });


    }
    
    function disapprovalPetition(pet_id) {
        var task = "disapproval";

        $.post('process_status_salary.php', {task: task, pet_id: pet_id}, function(data) {
            window.location.href = "index.php?pages=verify_approved_salary";
        });

    }


</script>


<a href="index.php?pages=frm_petition_salary_add" class="btn" id="add_new">เพิ่มฎีกาเงินเดือน</a>
<table id="example" class="table table-bordered" width="100%" >
    <caption><br></caption>
    <thead>
        <tr class="error">
            <th>#</th>
            <th>เลขที่ฎีกา</th>
            <th>แผนงาน</th>
            <th>งาน</th>
            <th>ประเภท</th>
            <th>รายการ</th>
            <th>จำนวนเงิน</th>
            <th>action</th>
            <th>อนุมัติ</th>
            
        </tr>
    </thead>
    <tbody>	
        <?php
        $i = 0;
        while ($rows = $db->fetch_object($result)) {
            $i++;

            
            //echo "<tr class=''>
            echo ($rows->status == "ยกเลิก")? "<tr class='error'>":"<tr>";
            echo "
                    <td>$i</td>
                    <td>$rows->pet_id</td>
                    <td>$rows->panngan2_name</td>
                    <td>$rows->panngan3_name</td>
                    <td>$rows->expenses2_name</td>
                    <td>$rows->expenses3_name</td>
                    <td>" . number_format($rows->salary, 2) . "</td>
                    <td><a href='index.php?pages=frm_petition_salary_edit&pageid=10&pet_id=$rows->pet_id'><i class='icon-pencil'></i></a>&nbsp;|&nbsp;<a href='javascript:cancel(\"$rows->pet_id\")'><i class='icon-remove'></i></a>&nbsp;|&nbsp;<a href='prt_salary?pet_id=$rows->pet_id' target='_blank'><i class='icon-print'></i></a></td>";
            
            echo "<td><p class='text-center'>";
            if ($rows->verify_status == "ผ่าน") {
                if ($rows->approved_status == "รออนุมัติ") {
                    echo "<a href='javascript:approvedPetition(\"$rows->pet_id\")' data-toggle='tooltip' title='อนุมัติ'><i class='icon-ok'></i></a>&nbsp;<big>|</big>&nbsp;<a href='javascript:disapprovalPetition(\"$rows->pet_id\")' data-toggle='tooltip' title='ไม่อนุมัติ'><i class='icon-remove'></i></a>";
                } else {
                    if ($rows->approved_status == "อนุมัติ") {
                        echo "<font color='blue'>";
                    }
                    echo $rows->approved_status;
                }
            } else {
                echo "รอการตรวจสอบ";
            }
            echo "</p></td>";
            echo "</tr>
            ";
        }
        ?>

    </tbody>
</table>
